﻿using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class List12TableStyleClientInfo : List12RecordItem
    {
        private XLUnicodeString _stListStyleName;
        private byte unused = 112;
        private bool unused21 = true;

        public List12TableStyleClientInfo(Table table)
        {
            Contract.Requires(table != null);
            if (table.Style != null)
                _stListStyleName = (XLUnicodeString) table.Style.Name;
            this.FColumnStripes = table.ColumnStripesVisible;
            this.FFirstColumn = table.FirstColumnVisible;
            this.FRowStripes = table.RowStripesVisible;
            this.FDefaultStyle = true;
            this.FLastColumn = table.LastColumnVisible;
        }

        public List12TableStyleClientInfo()
        {
        }

        protected byte Af
        {
            get
            {
                byte result = 0;
                if (FFirstColumn) result |= 0x1;
                if (FLastColumn) result |= 0x2;
                if (FRowStripes) result |= 0x4;
                if (FColumnStripes) result |= 0x6;
                if (FDefaultStyle) result |= 0x40;
                if (unused21) result |= 0x80;
                return result;
            }
        }

        public override List12RecordItemType Type
        {
            get { return List12RecordItemType.List12TableStyleClientInfo; }
        }

        public bool FRowStripes { get; private set; }

        public bool FLastColumn { get; private set; }

        public bool FFirstColumn { get; private set; }

        public bool FDefaultStyle { get; private set; }

        public bool FColumnStripes { get; private set; }

        public string StListStyleName
        {
            get { return (string) _stListStyleName; }
        }

        public override void Write(BinaryWriter writer)
        {
            writer.Write(Af);
            writer.Write(unused);
            _stListStyleName.Write(writer);
        }

        public override void Read(BinaryReader reader)
        {
            byte af = reader.ReadByte();
            FFirstColumn = (af & 0x1) != 0;
            FLastColumn = (af & 0x2) != 0;
            FRowStripes = (af & 0x4) != 0;
            FColumnStripes = (af & 0x8) != 0;
            FDefaultStyle = (af & 0x40) != 0;
            this.unused21 = (af & 0x80) != 0;
            Contract.Assert(Af == af);
            unused = reader.ReadByte();
            _stListStyleName = new XLUnicodeString();
            _stListStyleName.Read(reader);
        }
    }
}